package team.first.movieapiv2pro.controller;

import cn.hutool.core.convert.Convert;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import team.first.movieapiv2pro.domain.Movie;
import team.first.movieapiv2pro.dto.RequestMovie;
import team.first.movieapiv2pro.service.MovieService;
import team.first.movieapiv2pro.vo.ApiResponse;
import team.first.movieapiv2pro.vo.ApiResponsePaging;

import java.util.List;

/**
 * @Author: HAPPY
 * @Project_name: project
 * @Package_name: team.first.movieapibootv2.controller
 * @Date: 2021/11/19 7:39
 * @Description：
 */
@RestController
public class MovieController {
    @Autowired
    MovieService movieService;

    public static int totalRow = 0;

    /**
     * 1.2 获取指定编号的电影信息
     * 接口地址：GET /api/v2/movie/{movieId}
     */
    @ApiOperation("1.2 GET: 获取指定编号的电影信息")
    @GetMapping("/api/v2/movie/{movieId}")
    public ApiResponse<Movie> getMovieById(@PathVariable("movieId") Integer movieId) {
        ApiResponse<Movie> apiResponse = new ApiResponse<>();
        Movie movie = movieService.getById(movieId);
        apiResponse.setCode(200).setSuccess(true).setMessage("请求成功").setData(movie);
        return apiResponse;
    }

    /**
     * 2.3 根据条件分页获取指定的电影列表
     * 接口地址:POST /api/v2/movies
     */
    @PostMapping("/api/v2/movies")
    @ApiOperation("1.3 POST: 根据条件分页获取指定的电影列表")
    public ApiResponsePaging<List<Movie>> movies(RequestMovie requestMovie) {
        ApiResponsePaging<List<Movie>> apiResponsePaging = new ApiResponsePaging<>();

        List<Movie> movieList = movieService.getMovie(requestMovie);

        apiResponsePaging.setCode(200).setSuccess(true).setMessage("请求成功").setData(movieList).setTotalRow(totalRow);

        if (requestMovie.getPagerSize() != null) {
            apiResponsePaging.setPageSize(requestMovie.getPagerSize())
                    .setTotalPage(Convert.toInt(Math.ceil((double) totalRow / requestMovie.getPagerSize())));
        }
        if (requestMovie.getPageNumber() != null) {
            apiResponsePaging.setPageNumber(requestMovie.getPageNumber());
        }

        return apiResponsePaging;
    }
}
